package com.zjs.jsp.status.service;

import com.zjs.jsp.domain.stats.OrderStatus;
import com.zjs.jsp.domain.stats.StatusRoot;
import com.zjs.jsp.status.configuration.CustomerBs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author 李文
 * @create 2018-07-23 15:23
 **/
@Service
@ConfigurationProperties
public class SendCloudService
{
    private static final Logger LOGGER = LoggerFactory.getLogger(SendCloudService.class);

    @Autowired
    private CustomerBs customerBs;

    @Autowired
    RestTemplate restTemplate;

    public Boolean PushMessageHandle(String uuid, OrderStatus s) {
        String url = null;
        String r = null;
        try {
            url = "http://" + customerBs.getBs().get(s.getCustomerBs()) + "/OrderStatus/";
            s.setId(uuid);
            LOGGER.info("{} {} 发送数据为 {}", uuid, url, s.toString());
            StatusRoot statusRoot = restTemplate.postForObject(url, s, StatusRoot.class);
            if (statusRoot != null) {
                r = statusRoot.toString();
                LOGGER.info("{} {} 响应数据为 {}", uuid, url, r);
                if ("Y".equals(statusRoot.getStatus())) {
                    return true;
                } else {
                    LOGGER.error("{} 调用{} 失败  响应数据{}", uuid, url, r);
                }
            } else {
                throw new Exception(" 响应结果为空 ");
            }
        } catch (Exception e) {
            LOGGER.error("{} 调用{} 失败  响应数据{}", uuid, url, r, e);
        }
        return false;
    }
}
